/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlers;

import gestionnaires.GestionCommentaire;
import gestionnaires.GestionFilm;
import gestionnaires.GestionGenre;
import gestionnaires.GestionUtilisateur;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modeles.Commentaire;
import modeles.Film;
import modeles.Genre;
import modeles.Utilisateur;

/**
 *
 * @author mazid
 */
@WebServlet(name="ServletFilm", urlPatterns={"/servletfilms"})
public class ServletFilm extends HttpServlet {
    @EJB
    private GestionUtilisateur gestionUtilisateur;
    @EJB
    private GestionCommentaire gestionCommentaire;
    @EJB
    private GestionGenre gestionGenre;
    @EJB
    private GestionFilm gestionFilm;
    
    

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            
            String action = request.getParameter("action");
            System.out.println(action);
            String forward = "index.jsp?";      
            request.setAttribute("forwardTo", forward);
            Collection<Genre> genres = gestionGenre.getAllGenre();
            request.setAttribute("itemsGenres", genres);
            
            if(action==null ||action.equals("index")){
                accueil(request);
          
            }
            else if(action.equals("lister")){
                lister(request);
                request.setAttribute("includes", "filmsGenres");
            }
            else if(action.equals("ajouterComment")){
               
                ajouterComment(request);
                
                request.setAttribute("includes", "ajoutCommentaire");
                request.setAttribute("comments", "lister");
                response.sendRedirect(request.getHeader("Referer"));
            }
            else if(action.equals("noter")){
                request.setAttribute("includes", "filmsNotes");
            }
            else if(action.equals("afficher")){
                request.setAttribute("includes", "filmsAffiches");
                
            }else if(action.equals("commenter")){
                long idFilm = Integer.parseInt(request.getParameter("idFilm"));
                Film film = gestionFilm.getFilmById(idFilm);
                request.setAttribute("film", film);
                request.setAttribute("comments", "ajouter");
                request.setAttribute("includes", "ajoutCommentaire");
            }
            RequestDispatcher rd = request.getRequestDispatcher(forward);
            rd.forward(request, response);
            
        } catch (Exception e) {
            
        }
        
    }
    public void accueil(HttpServletRequest request){
        Collection<Film> films = gestionFilm.getAllFilm();
        request.setAttribute("listFilms", films);
        
        
    }
    
    private void lister(HttpServletRequest request) {
       int idGenre = Integer.parseInt(request.getParameter("idGenre"));
       Collection<Film> filmsGenre = gestionFilm.getFilmParGenre(idGenre);
       request.setAttribute("filmParGenre", filmsGenre);
    }
    private void ajouterComment(HttpServletRequest request) {
        System.out.println("entréeeee");
        long idFilm = Integer.parseInt(request.getParameter("idFilm"));
        System.out.println(idFilm);
        String loginUser = request.getParameter("loginUser");
        System.out.println(loginUser);
        String commentaire = request.getParameter("commentaire");
        System.out.println(commentaire);
        Film f = gestionFilm.getFilmById(idFilm);
        System.out.println(f.getTitre());
        Utilisateur u = gestionUtilisateur.getUserParLogin(loginUser);
        System.out.println(u.getNomU());
        Commentaire c = gestionCommentaire.ajouterCommentaire(commentaire, f, u);
        System.out.println(c.getCommentaire()+" "+c.getFilm().getTitre()+" "+c.getUtilisateur().getNomU());
        System.out.println(idFilm+" ; "+loginUser);
       
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    
    
}
